﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Explore
{
    public partial class ShowImage : Form
    {
        public ShowImage()
        {
            InitializeComponent();
        }
        private Bitmap image = null;
        private float angle = 0.0f;
        public static string pathImage;// = @"F:\PIC\DCIM\Temp3\P1010559.JPG";
        string path = pathImage.Substring(0, pathImage.LastIndexOf("\\"));
        Stack BackImage = new Stack();
        Stack ForImage = new Stack();
        Stack Temp = new Stack();

        private void ShowImage_Load(object sender, EventArgs e)
        {
            int i = 0;
            DirectoryInfo dirlistview = new DirectoryInfo(path);
            foreach (FileInfo file in dirlistview.GetFiles())
            {
                if (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".png")
                {
                    if (file.FullName == pathImage) i = 1;
                    if (i == 0)
                        BackImage.Push(file.FullName);
                    if (i == 1) Temp.Push(file.FullName);
                }
            }
            while (Temp.Count > 0) ForImage.Push(Temp.Pop());
            image = new Bitmap(pathImage);
            pictureBox.Image = (Bitmap)image.Clone();

        }


        private void btn_rot_left_Click(object sender, EventArgs e)
        {
            image = (Bitmap)pictureBox.Image;
            angle = angle - 90.0f;
            RotateImage(pictureBox, image, angle);
        }

        private void btn_rot_right_Click(object sender, EventArgs e)
        {
            image = (Bitmap)pictureBox.Image;
            angle = angle + 90.0f;
            RotateImage(pictureBox, image, angle);
        }

        #region "Ratate"
        //Refer: http://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations

        private void RotateImage(PictureBox pb, Image img, float angle)
        {
            if (img == null || pb.Image == null)
                return;

            Image oldImage = pb.Image;
            pb.Image = RotateImage(img, angle);
            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }
        public static Bitmap RotateImage(Image image, PointF offset, float angle)
        {
            if (image == null)
                throw new ArgumentNullException("image");

            //create a new empty bitmap to hold rotated image
            Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
            rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            //make a graphics object from the empty bitmap
            Graphics g = Graphics.FromImage(rotatedBmp);

            //Put the rotation point in the center of the image
            g.TranslateTransform(offset.X, offset.Y);

            //rotate the image
            g.RotateTransform(angle);

            //move the image back
            g.TranslateTransform(-offset.X, -offset.Y);

            //draw passed in image onto graphics object
            g.DrawImage(image, new PointF(0, 0));

            return rotatedBmp;
        }
        public static Bitmap RotateImage(Image image, float angle)
        {
            return RotateImage(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
        }
        #endregion

        private void btn_Back_Click(object sender, EventArgs e)
        {
            if (BackImage.Count == 0) return;
            string str = BackImage.Pop().ToString();
            pictureBox.Image = Image.FromFile(str);
            ForImage.Push(str);
        }

        private void btn_Forw_Click(object sender, EventArgs e)
        {
            if (ForImage.Count == 0) return;
            string str = ForImage.Pop().ToString();
            pictureBox.Image = Image.FromFile(str);
            BackImage.Push(str);
        }



    }
}
